package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.listener.OrderItemClickListener;
import com.hemaapp.yjnh.bean.Order;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：买家预约订单适配器
 * 创建人：GuiyouZhang
 * 创建时间：2016/7/20 17:22
 * 修改备注：
 */
public class BookOrderListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<Order> datas;
    private Context mContext;
    private OrderItemClickListener clickListener;

    public BookOrderListAdapter(Context context, ArrayList<Order> orders, OrderItemClickListener clickListener) {
        this.mContext = context;
        this.datas = orders;
        this.clickListener = clickListener;
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.size() == 0)
            return -1;
        else
            return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.listitem_empty_base, parent, false);
            return new EmptyViewHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_order_book, parent, false);
            return new ViewHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (datas.size() == 0) {
            if (holder instanceof EmptyViewHolder)
                ((EmptyViewHolder) holder).tvEmtpy.setText("暂时没有订单");
        } else {
            if (holder instanceof ViewHolder) {
                ((ViewHolder) holder).setDatas(position);
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas.size() == 0 ? 1 : datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_order_num)
        TextView tvOrderNum;
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.layout_goods)
        LinearLayout layoutGoods;
//        @Bind(R.id.tv_freight)
//        TextView tvFreight;
        @Bind(R.id.tv_total_fee)
        TextView tvTotalFee;
        @Bind(R.id.tv_cancel)
        TextView tvCancel;
        @Bind(R.id.tv_conmment)
        TextView tvComment;
        @Bind(R.id.tv_delete)
        TextView tvDelete;

        int position = -1;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null && position != -1)
                        clickListener.onItemClickListener(position);
                }
            });
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null)
                        clickListener.onCancel(position);
                }
            });
            tvComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null)
                        clickListener.onPay(position);
                }
            });
            tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null)
                        clickListener.onRefusePay(position);
                }
            });
        }

        public void setDatas(int position) {
            this.position = position;
            Order order = datas.get(position);
            //根据订单状态来显示下方的操作按钮
            tvOrderNum.setText("订单号：" + order.getBill_sn());
//            tvFreight.setText("合计(含运费￥" + order.getShipping_fee() + ")：");
            tvTotalFee.setText(order.getTotal_fee());
            setStatusAndBottom(order);
            //动态添加多个商品
            if (layoutGoods.getChildCount() != 0) {
                layoutGoods.removeAllViews();
            }
            for (int i = 0; i < order.getChildren().size(); i++) {
                View view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_order_goods, null);
                ItemViewHolder ivHolder = new ItemViewHolder();
                itemFindView(view, ivHolder);
                setItem(position, this, i, order, view, ivHolder);
            }
        }

        /**
         * 设置订单状态和底部按钮
         *
         * @param order
         * @return
         */
        private void setStatusAndBottom(Order order) {
            if (order.getStatetype().equals("2")) {
                tvStatus.setText("交易关闭");
                setBottomButton(new int[]{View.GONE, View.GONE, View.GONE});
            }else {
                if (!order.getReturnflag().equals("0")) {//退货状态	0无, 非0有
                    tvStatus.setText("交易关闭");
                    setBottomButton(new int[]{View.GONE, View.GONE, View.GONE});
                    return;
                }
                //交易状态	0 未付款 1: 已付款待发货 2 待收货 3 已收货待评价 4 已评价交易成功
                if (order.getTradetype().equals("0")) {
                    tvStatus.setText("待付款");
                    setBottomButton(new int[]{View.GONE, View.GONE, View.GONE});
                } else if (order.getTradetype().equals("1")) {
                    tvStatus.setText("待消费");
                    setBottomButton(new int[]{View.VISIBLE, View.GONE, View.GONE});
                } else if (order.getTradetype().equals("2")) {
                    tvStatus.setText("待消费");
                    setBottomButton(new int[]{View.VISIBLE, View.GONE, View.GONE});
                } else if (order.getTradetype().equals("3")) {
                    tvStatus.setText("待评价");
                    setBottomButton(new int[]{View.GONE, View.GONE, View.VISIBLE});
                } else if (order.getTradetype().equals("4")) {
                    tvStatus.setText("交易成功");
                    setBottomButton(new int[]{View.GONE, View.GONE, View.GONE});
                } else {
                    tvStatus.setText("未知状态");
                    setBottomButton(new int[]{View.GONE, View.GONE, View.GONE});
                }
            }
        }

        /**
         * @param visibility
         * 依次为：取消订单、删除订单、评价
         */
        public void setBottomButton(int[] visibility) {
            tvCancel.setVisibility(visibility[0]);
            tvDelete.setVisibility(visibility[1]);
            tvComment.setVisibility(visibility[2]);
        }

        private class ItemViewHolder {
            private ImageView img;
            private TextView name;
            private TextView price;
            private TextView amount;
            private TextView tv_commit;//复用Layout，这里的评论和申请售后，显示为“同意”“拒绝”
            private TextView tv_aftersale;
            private View div;
            private TextView tv_hint;
        }

        /**
         * 一个订单中的多个商品
         *
         * @param view
         * @param ivHolder
         */
        private void itemFindView(View view, ItemViewHolder ivHolder) {
            ivHolder.img = (ImageView) view.findViewById(R.id.img);
            ivHolder.name = (TextView) view.findViewById(R.id.good_name);
            ivHolder.price = (TextView) view.findViewById(R.id.price);
            ivHolder.amount = (TextView) view.findViewById(R.id.count);
            ivHolder.tv_commit = (TextView) view.findViewById(R.id.tv_commit);
            ivHolder.tv_aftersale = (TextView) view.findViewById(R.id.tv_aftersale);
            ivHolder.div = view.findViewById(R.id.divider);
            ivHolder.tv_hint = (TextView) view.findViewById(R.id.tv_hint);
        }

        private void setItem(final int position, ViewHolder holder, int i, final Order bill,
                             View view, ItemViewHolder ivHolder) {
            Order.BillChild child = bill.getChildren().get(i);
            ImageUtils.loadSmalImage(mContext, child.getImgurl(), ivHolder.img);
            ivHolder.name.setText(child.getName());
            ivHolder.price.setText("¥" + child.getPrice());
            ivHolder.amount.setText("x" + child.getBuycount());
            holder.layoutGoods.addView(view);
            if (i == bill.getChildren().size() - 1) {
                ivHolder.div.setVisibility(View.GONE);
            } else {
                ivHolder.div.setVisibility(View.VISIBLE);
            }
            ivHolder.tv_commit.setVisibility(View.GONE);
            ivHolder.tv_aftersale.setVisibility(View.GONE);
        }
    }
}
